home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / mac / graficos / hpgl-to-pict-hc.hqx / hp2pict deck / card_5632.txt < prev    next >
Text File  |  1982-12-17  |  12KB  |  476 lines

  1. -- card: 5632 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 2253
  5. -- name: hp2pictXCMD.c
  6.  
  7.  
  8. -- part contents for background part 1
  9. ----- text -----
  10. hp2pictXCMD.c
  11.  
  12. -- part contents for background part 6
  13. ----- text -----
  14. /* hp2pictXCMD.c
  15.  *
  16.  *
  17.  * Copyright 1990 by d.g.gilbert.
  18.  * dogStar Software && Indiana University Biology Dept.
  19.  * email: gilbertd@iubio.bio.indiana.edu
  20.  *
  21.  * Language: MPW-C 3.0
  22.  *
  23.  * based in part on AuxWindow by Roger Brown, Dartmouth XCMD's 
  24.  *
  25.  */
  26.  
  27. /* Creates a floating, no go-away window with a picture that is
  28.    translated from HPGL graphics script
  29.  
  30.       Card syntax is:
  31.  
  32.   hp2pict "Open", name, [rect]   -- open blank window
  33.       * returns window pointer as The Result
  34.  
  35.   hp2pict "DrawF", window pointer, FILE w/ HPGL
  36.   hp2pict "DrawC", window pointer, CONTAINER w/ HPGL
  37.           -- draw hp contents to window
  38.  
  39.   hp2pict "Fetch", window pointer, Container w/ (partial) HPGL
  40.         -- draw hp while reading it from comm link thru
  41.           callback to HyperCard, using HC function "recvTo(,,,)"
  42.  
  43.   hp2pict "File",window pointer,filename -- save pict as file
  44.  
  45.   hp2pict "Clip",window pointer   -- put window pict into clipbd
  46.  
  47.   hp2pict "Print",window pointer  -- print window pict
  48.  
  49.   hp2pict "Close",window pointer  -- close & dispose of window
  50.  
  51. */
  52.  
  53. /*-------- Makefile
  54.  
  55. C -b hp2pictXCMD.c -mbg off
  56. Link -w -rt XCMD=10003 Γêé
  57.   -m ENTRYPOINT Γêé
  58.   -sg hp2pict Γêé
  59.   hp2pictXCMD.c.o Γêé
  60.   "{Libraries}HyperXLib.o" Γêé
  61.   "{Libraries}Interface.o" Γêé
  62.   "{CLibraries}StdCLib.o" Γêé
  63.   "{Clibraries}"CSANELib.o Γêé
  64.   "{Clibraries}"Cinterface.o Γêé
  65.   "{CLibraries}CRuntime.o" Γêé
  66.   -o "hp2pict deck"
  67. #-------------------------*/
  68.  
  69.  
  70.  
  71. /*#include <CType.h>*/
  72. #include <Types.h>
  73. #include <StdIO.h>
  74. #include <Sane.h>
  75. #include <QuickDraw.h>
  76. #include <FixMath.h>
  77. #include <Fonts.h>
  78. #include <Memory.h>
  79. #include <Scrap.h>
  80. #include <OSEvents.h>
  81. #include <OSUtils.h>
  82. #include <Files.h>
  83. #include <Resources.h>
  84. #include <Windows.h>
  85. #include <Events.h>
  86. #include <ToolUtils.h>
  87. #include <Dialogs.h>
  88. #include <PrintTraps.h>
  89. #include <HyperXCmd.h>
  90.  
  91.   /* draw formats */
  92. #define DFILE   1
  93. #define DCARD   2
  94. #define DCOMM   3
  95.  
  96. void  returnValue(XCmdPtr pXCmd, char *theResult);
  97. void  upcase(char *s);
  98. void  OpenHPWind(XCmdPtr pXCmd);
  99. void  DrawHPWind(XCmdPtr pXCmd, int format);
  100. void  FileHPWind(XCmdPtr pXCmd);
  101. void  ClipHPWind(XCmdPtr pXCmd);
  102. void  PrintHPWind(XCmdPtr pXCmd);
  103. void  CloseHPWind(XCmdPtr pXCmd);
  104.  
  105.  
  106. /* XCMD entry -- MUST BE FIRST CODE IN SEG */
  107.  
  108. pascal void EntryPoint(XCmdPtr pXCmd)
  109. {
  110.   Ptr       theMessage;
  111.   char      message[32];
  112.   GrafPtr   cardport;
  113.  
  114.   GetPort( &cardport);
  115.   theMessage = *(pXCmd->params[0]);
  116.   strcpy(message,theMessage);
  117.   upcase(message);
  118.   pXCmd->inArgs[0] = (long) cardport; /* global for subs */
  119.  
  120.   if (strcmp(message,"OPEN")==0)        OpenHPWind(pXCmd);
  121.   else if (strcmp(message,"DRAWF")==0)  DrawHPWind(pXCmd, DFILE);
  122.   else if (strcmp(message,"DRAWC")==0)  DrawHPWind(pXCmd, DCARD);
  123.   else if (strcmp(message,"FETCH")==0)  DrawHPWind(pXCmd, DCOMM);
  124.   else if (strcmp(message,"FILE")==0)   FileHPWind(pXCmd);
  125.   else if (strcmp(message,"CLIP")==0)   ClipHPWind(pXCmd);
  126.   else if (strcmp(message,"PRINT")==0)  PrintHPWind(pXCmd);
  127.   else if (strcmp(message,"CLOSE")==0)  CloseHPWind(pXCmd);
  128.   else {
  129.     SysBeep(0);
  130.     returnValue( pXCmd, "Undefined message in hp2pict XCMD");
  131.     }
  132.   SetPort( cardport);
  133.   return;
  134. }
  135.  
  136.  
  137. #define min(a,b)  (a<b) ? a : b
  138. #define max(a,b)  (a>b) ? a : b
  139. #define Isalpha(c)    ((c >= 'A' & c <= 'Z') \
  140.                     |  (c >= 'a' & c <= 'z'))
  141.  
  142. int Toupper(char c)
  143. {
  144.   return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c );
  145. }
  146.  
  147.  
  148. void upcase(char *s)
  149. {
  150.   int i,l;
  151.   l = strlen(s);
  152.   for (i=0;i<l;i++) s[i] = Toupper(s[i]);
  153. }
  154.  
  155. float  Strtod(char *s, char **ends)
  156. {  /* stdlib strtod, strtol use %GlobalData !! */
  157.   float   x;
  158.   short   e;
  159.   decimal drec;
  160.   short   valid;
  161.  
  162.   e = 0;
  163.   str2dec( s, &e, &drec, &valid);
  164.   x = dec2num( &drec);
  165.   *ends = s + e;
  166.   return x;
  167. }
  168.  
  169.  
  170. void  str2rect(char *c, Rect *r)
  171. /* convert HC rect string to Rect */
  172. {
  173.   r->left   = Strtod( c, &c); c++;
  174.   r->top    = Strtod( c, &c); c++;
  175.   r->right  = Strtod( c, &c); c++;
  176.   r->bottom = Strtod( c, &c);
  177. }
  178.  
  179.  
  180. Boolean hasColorQD(void)
  181. {
  182.   SysEnvRec theWorld;
  183.   int      err;
  184.  
  185.   err = SysEnvirons(1,&theWorld);
  186.   if (err!=0) return false;
  187.   else return theWorld.hasColorQD;
  188. }
  189.  
  190. #define lotsOfColor(wind) \
  191.     ( (**((CGrafPtr)wind)->portPixMap).pixelSize > 7 )
  192.  
  193.  
  194. void  returnValue(XCmdPtr pXCmd, char *theResult)
  195. {
  196.   long len;
  197.   Handle resultHandle;
  198.  
  199.   /* return the result to HCard */
  200.   len = 1+strlen(theResult);
  201.   resultHandle = NewHandle(len);
  202.   HLock(resultHandle);
  203.   BlockMove(theResult,*resultHandle,len);
  204.   HUnlock(resultHandle);
  205.   pXCmd->returnValue = resultHandle;
  206. }
  207.  
  208.  
  209.  
  210. void OpenHPWind(XCmdPtr pXCmd)
  211. {
  212.   char      *theName, *c, theResult[256];
  213.   Rect      wr;
  214.   Boolean   hascolor;
  215.   WindowPtr myWind;
  216.  
  217.   theName = *(pXCmd->params[1]);
  218.   hascolor = hasColorQD();
  219.   c = *(pXCmd->params[2]);
  220.   if ((c != NULL) && (*c != 0)) {
  221.     str2rect( c, &wr);
  222.     LocalToGlobal( (Point *)&wr.top);
  223.     LocalToGlobal( (Point *)&wr.bottom);
  224.     }
  225.   else
  226.     SetRect( &wr, 10, 80, 446, 370);  /* scaled size for 9" SE monitor */
  227.  
  228.   /*?? use wStorage == newhandle/movehhi instead of newwindow ptr */
  229.   if (hascolor)
  230.     myWind = newcwindow( NULL, &wr, theName, true,
  231.                 noGrowDocProc, (WindowPtr)-1, false, 0);
  232.   else
  233.     myWind = newwindow( NULL, &wr, theName, true,
  234.                 noGrowDocProc, (WindowPtr)-1, false, 0);
  235.   SetPort(myWind);
  236.  
  237.   numtostring( myWind,theResult);
  238.   returnValue( pXCmd, theResult);
  239.   ShowWindow( myWind);
  240.   BringToFront( myWind);
  241.   SetWRefCon( myWind, 0L);
  242.   SetWindowPic( myWind, NULL);
  243. }
  244.  
  245.  
  246. void CloseHPWind(XCmdPtr pXCmd)
  247. {
  248.   char *theWindowPtr;
  249.   WindowPtr myWind;
  250.   PicHandle cardPic, pagePic;
  251.  
  252.   theWindowPtr = *(pXCmd->params[1]);
  253.   stringtonum(theWindowPtr,&myWind);
  254.   cardPic = GetWindowPic(myWind);
  255.   if (cardPic != 0L) KillPicture(cardPic);
  256.   pagePic = (PicHandle)GetWRefCon(myWind);
  257.   if (pagePic != 0L) KillPicture(pagePic);
  258.   DisposeWindow(myWind);
  259.   returnValue( pXCmd, "0");
  260. }
  261.  
  262.  
  263.  
  264.  
  265. #include "hp2pict.inc.c"
  266.  
  267.  
  268.  
  269.  
  270.  
  271. void DrawHPWind(XCmdPtr pXCmd, int format)
  272. {
  273.   char    *c, *buf, *fname, *theWindowPtr, theResult[256];
  274.   Handle  hphand;
  275.   long    i, hplen;
  276.   WindowPtr myWind;
  277.   PicHandle cardPic, pagePic;
  278.   int     err;
  279.   short   fref;
  280.   Rect    windrect, pagerect;
  281.   Boolean alldone;  /* end of all plots -> multipage */
  282.  
  283.  
  284.   theWindowPtr = *(pXCmd->params[1]);
  285.   stringtonum(theWindowPtr,&myWind);
  286.   SetPort(myWind);
  287.   cardPic = GetWindowPic(myWind);
  288.   if (cardPic != 0L) KillPicture(cardPic);
  289.   cardPic = 0L;
  290.   pagePic = (PicHandle)GetWRefCon(myWind);
  291.   if (pagePic != 0L) KillPicture(pagePic);
  292.   pagePic = 0L;
  293.  
  294.   if (format == DFILE) {
  295.     /* --- read HP code from a file ---- */
  296.     fname = *(pXCmd->params[2]);
  297.     if (fsopen( fname, 0, &fref) != 0) {
  298.       strcpy( theResult, "HPDraw, file not found: ");
  299.       strcat( theResult, fname);
  300.       returnValue( pXCmd, theResult);
  301.       return;
  302.       }
  303.     err = GetEOF( fref, &hplen);
  304.     hphand = NewHandle(hplen);
  305.     HLock(  hphand);
  306.     err = FSRead( fref, &hplen, *hphand);
  307.     err = FSClose( fref);
  308.     HUnlock( hphand);
  309.     SetHandleSize( hphand, hplen);
  310.     }
  311.   else {
  312.     hphand = pXCmd->params[2];
  313.     HandToHand( &hphand);
  314.     hplen = GetHandleSize(hphand);
  315.     }
  316.  
  317.   buf = *hphand;
  318.   /* fix any crud in container */
  319.   /* !! hi ascii is missed or dropped in C comparisons */
  320.   for (i = 0, c = buf; i<hplen; i++) {
  321.     if ( buf[i] == 3) *c++ = LBEND;
  322.     else if (buf[i] == LBEND | buf[i] >= 27) *c++ = buf[i];
  323.     }
  324.   *c = 0;
  325.   hplen = c - buf + 1;
  326.   SetHandleSize( hphand, hplen+1000); /* !! +1000 debug */
  327.  
  328.   /* windrect = HCard scaled size */
  329.   windrect = myWind->portRect;
  330.   cardPic = hp2pict( false, &windrect, hphand, myWind,
  331.                      format == DCOMM, &alldone, pXCmd);
  332.  
  333.   /* pagerect = lw scaled-landscape size */
  334.   SetRect( &pagerect, 0, 20, 730, 532);
  335.   /* -- Note: commfetch is false 2nd time thru -- */
  336.   pagePic = hp2pict(  true, &pagerect, hphand, myWind,
  337.                      false, &alldone, pXCmd);
  338.  
  339.   HUnlock( hphand);
  340.   pXCmd->returnValue = hphand; /* ? return for debugging */
  341.   /*
  342.   DisposHandle(hphand);
  343.   returnValue( pXCmd, "0");
  344.   */
  345.   if ((pagePic != 0L)
  346.    & GetHandleSize((Handle)pagePic) > 0L )
  347.     SetWRefCon(myWind,(long)pagePic);
  348.   else
  349.     SetWRefCon(myWind,0);
  350.  
  351.   if ((cardPic != 0L)
  352.    & GetHandleSize((Handle)cardPic) > 0L ) {
  353.     SetWindowPic(myWind,cardPic);
  354.     BringToFront(myWind);
  355.     }
  356.   else
  357.     SetWindowPic(myWind,NULL);
  358.  
  359. }
  360.  
  361.  
  362. void FileHPWind(XCmdPtr pXCmd)
  363. {
  364.   #define headsize  512
  365.   char  head[headsize];
  366.   int   n, err;
  367.   short fref;
  368.   char  *fname, *theWindowPtr;
  369.   WindowPtr myWind;
  370.   PicHandle pagePic;
  371.  
  372.   theWindowPtr = *(pXCmd->params[1]);
  373.   stringtonum(theWindowPtr,&myWind);
  374.   SetPort(myWind);
  375.   fname = *(pXCmd->params[2]);
  376.   pagePic = (PicHandle)GetWRefCon(myWind);
  377.   if (pagePic == 0L) pagePic = GetWindowPic(myWind);
  378.   if (pagePic != 0L) {
  379.     /* creator 'MDPL' == MacDraw II */
  380.     err = create( fname, 0, 'MDPL', 'PICT');
  381.     err = fsopen( fname, 0, &fref);
  382.     err = SetEOF( fref, 0);
  383.     for (n=0; n<headsize; n++) head[n] = 0;
  384.     n = headsize;
  385.     err = FSWrite( fref, &n, head);
  386.     n = GetHandleSize( (Handle) pagePic);
  387.     HLock( (Handle)pagePic);
  388.     err = FSWrite( fref, &n, *(Handle)pagePic);
  389.     HUnlock( (Handle)pagePic);
  390.     err = FSClose( fref);
  391.     }
  392. }
  393.  
  394.  
  395.  
  396.  
  397.  
  398. void ClipHPWind(XCmdPtr pXCmd)
  399. {
  400.   char  *theWindowPtr;
  401.   WindowPtr myWind;
  402.   PicHandle cardPic;
  403.  
  404.   theWindowPtr = *(pXCmd->params[1]);
  405.   stringtonum(theWindowPtr,&myWind);
  406.   SetPort(myWind);
  407.   cardPic = GetWindowPic(myWind);
  408.   /* pagePic = (PicHandle)GetWRefCon(myWind); */
  409.   if (cardPic != 0L) {
  410.     ZeroScrap();
  411.     HLock((Handle)cardPic);
  412.     PutScrap(GetHandleSize((Handle)cardPic), 'PICT', *(Handle)cardPic);
  413.     HUnlock((Handle)cardPic);
  414.     }
  415. }
  416.  
  417.  
  418.  
  419. void PrintHPWind(XCmdPtr pXCmd)
  420. {
  421. #define rStopPrintDlog  102   /* this is HCard's PrintIdl dlog */
  422.   char        *theWindowPtr;
  423.   WindowPtr   myWind;
  424.   GrafPtr     oldPort;
  425.   TPPrPort    prPort;
  426.   TPrStatus   prStat;
  427.   Boolean     okay  = false;
  428.   THPrint     hprec;
  429.   PicHandle   pagePic;
  430.   DialogPtr   canDlog;
  431.   Str255      wname;
  432.  
  433.  
  434.   theWindowPtr = *(pXCmd->params[1]);
  435.   stringtonum(theWindowPtr,&myWind);
  436.   SetPort(myWind);
  437.   pagePic = (PicHandle)GetWRefCon(myWind);
  438.   if (pagePic == 0L) pagePic = GetWindowPic(myWind);
  439.   returnValue( pXCmd, "0");
  440.   if (pagePic != 0L) {
  441.     GetPort(&oldPort);
  442.     PrOpen();
  443.     if (PrError() == 0) {
  444.       hprec = (THPrint) NewHandle(sizeof(TPrint));
  445.       PrintDefault( hprec);
  446.       PrStlDialog( hprec);  /* ?? save hprec b/n calls here ? */
  447.       if (! PrJobDialog( hprec)) return; /* cancelled by user */
  448.  
  449.       SendHCMessage(pXCmd, "\pset cursor to watch");
  450.       GetWTitle(myWind, wname);
  451.       ParamText(wname,'','','');
  452.       canDlog = GetNewDialog(rStopPrintDlog, NULL, (WindowPtr)-1);
  453.       DrawDialog(canDlog);
  454.  
  455.       prPort = PrOpenDoc( hprec, NULL, NULL);
  456.       SetPort( (GrafPtr)prPort);
  457.       PrOpenPage(prPort,NULL);
  458.       DrawPicture( pagePic, &(**pagePic).picFrame);
  459.       PrClosePage( prPort);
  460.       PrCloseDoc( prPort);
  461.       if ( ((**hprec).prJob.bJDocLoop == bSpoolLoop)
  462.            & (PrError() == 0) )
  463.           PrPicFile(hprec, NULL,NULL,NULL, &prStat);
  464.       if (PrError() == 0) okay = true;
  465.       DisposHandle( (Handle)hprec);
  466.       DisposDialog( canDlog);
  467.       }
  468.     PrClose();
  469.     SetPort(oldPort);
  470.     }
  471.   if (!okay) returnValue( pXCmd, "Printing error");
  472. } /* PrintWindow */
  473.  
  474.  
  475.  
  476.